package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.xrit.sunnyday.dao.PermissionDao;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.pojo.Permission;
import com.xrit.sunnyday.pojo.User;
import com.xrit.sunnyday.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 权限管理
 */
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    /**
     * 权限条件分页查询
     *
     * 1 使用静态方法，pageheleper
     * 2 紧接着的查询语句会被分页
     * 3 Page 是pageHelper对象, 分页信息
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //拼接
        if (!StringUtil.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+ queryPageBean.getQueryString()+ "%");
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Permission> permissionList = permissionDao.findPage(queryPageBean.getQueryString());
        PageInfo page = new PageInfo(permissionList);
        // 包装到PageResult 再返回
        // 1 解耦
        // 2 total是基本类型，丢失。
        // 3 page对象内容过多 total,rows
        return new PageResult(page.getTotal(),page.getList());
    }

    /**
     * 添加权限
     *
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 通过id查询权限
     *
     * @param id
     * @return
     */
    @Override
    public Permission findById(int id) {
        return permissionDao.findById(id);
    }

    /**
     * 修改权限
     *
     * @param permission
     */
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    /**
     * 删除权限
     *
     * @param id
     */
    @Override
    public void delete(int id) {
        permissionDao.delete(id);
    }
}
